package com.klm.netty.handler;

//import com.klm.proto.BinaryRequest;
//import com.klm.proto.BinaryResponse;
import lombok.extern.slf4j.Slf4j;
        import org.springframework.stereotype.Component;

/**
 * @author wq
 */
@Slf4j
@Component
public class TranslateMessageHandler{
//public class TranslateMessageHandler extends SimpleChannelInboundHandler<BinaryRequest> {

//    @Autowired
//    private ApplicationContext applicationContext;
//
//    @Autowired
//    private ObjectMapper objectMapper;
//
//    @Override
//    protected void channelRead0(ChannelHandlerContext ctx, BinaryRequest request) {
//        String requestId = UUID.randomUUID().toString();
//        long startTime = System.currentTimeMillis();
//
//        log.info("[Binary Request] [{}] Received request for path: {}", requestId, request.getPath());
//
//        try {
//            // 获取请求路径对应的Controller方法
//            RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
//            Map<RequestMappingInfo, HandlerMethod> handlerMethods = mapping.getHandlerMethods();
//
//            // 查找匹配的Controller方法
//            HandlerMethod handlerMethod = null;
//            for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
//                if (entry.getKey().getPatternsCondition().getPatterns().contains(request.getPath())) {
//                    handlerMethod = entry.getValue();
//                    break;
//                }
//            }
//
//            if (handlerMethod == null) {
//                log.warn("[Binary Request] [{}] No handler found for path: {}", requestId, request.getPath());
//                sendErrorResponse(ctx, 404, "No handler found for path: " + request.getPath());
//                return;
//            }
//
//            log.debug("[Binary Request] [{}] Found handler: {}", requestId, handlerMethod.getMethod().getName());
//
//            // 获取Controller实例
//            Object controller = applicationContext.getBean(handlerMethod.getBeanType());
//
//            // 解析请求参数
//            Method method = handlerMethod.getMethod();
//            Class<?> paramType = method.getParameterTypes()[0];
//            Object param = objectMapper.readValue(request.getRequestData().toByteArray(), paramType);
//
//            log.debug("[Binary Request] [{}] Parsed request parameters: {}", requestId, param);
//
//            // 调用Controller方法
//            Object result = method.invoke(controller, param);
//
//            // 构建响应
//            BinaryResponse response = BinaryResponse.newBuilder()
//                    .setCode(200)
//                    .setMessage("Success")
//                    .setData(ByteString.copyFrom(objectMapper.writeValueAsBytes(result)))
//                    .build();
//            ctx.writeAndFlush(response);
//
//            long duration = System.currentTimeMillis() - startTime;
//            log.info("[Binary Request] [{}] Request completed successfully in {}ms", requestId, duration);
//
//        } catch (Exception e) {
//            long duration = System.currentTimeMillis() - startTime;
//            log.error("[Binary Request] [{}] Error handling request after {}ms: {}", requestId, duration, e.getMessage(), e);
//            sendErrorResponse(ctx, 500, "Internal server error: " + e.getMessage());
//        }
//    }
//
//    private void sendErrorResponse(ChannelHandlerContext ctx, int code, String message) {
//        BinaryResponse response = BinaryResponse.newBuilder()
//                .setCode(code)
//                .setMessage(message)
//                .build();
//        ctx.writeAndFlush(response);
//    }
}
